/**
 * Object for text formatting which is accepted by the Formatter object
 * @author Nestor Lumanas
 */


/**
TAG IN SYLLABUS						TAG IN FORMAT

code								code
title								title
faculty								faculty
description							description
term								schedule
time								schedule
room								schedule
prereqs,pre							prereqs
postreqs,post						postreqs
objectives,objective,preamble		objectives
requirements						requirements
materials							materials
references							references
assessments							assessments
topics,topic,week,name,hour			topics

*NOTE: Headers' font styles are automatically set to bold, but retain their family,size,color and alignment
*NOTE: Program automatically reads defaultFormat.xml in the root folder
 */

package org.dlsu.pdfgen;

import java.util.HashMap;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Paragraph;

public class Format {
	
	private  HashMap<String, Text> fontlist = new HashMap<String, Text>();
	private  Font defaultNormal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
	private  Font defaultBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
	private  Rectangle pagesize;
	private Format()
	{
	
	}
	
	public Format(Font defaultNormal, Font defaultBold)
	{
		this();
		this.defaultNormal = defaultNormal;
		this.defaultBold = defaultBold;
		pagesize = PageSize.LETTER;
	}
	
	public Format(Font defaultNormal, Font defaultBold, Rectangle ps)
	{
		this(defaultNormal,defaultBold);
		pagesize = ps;
	}

	public boolean add(String name, String family, String style, String color, float size, String alignment)
	{
		Font f = new Font(fontfamily(family),size,style(style),color(color));
		return add(name, f, alignment);
	}
	
	public boolean add(String name, String family, String style, String color, float size)
	{
		Font f = new Font(fontfamily(family),size,style(style),color(color));
		return add(name,f);
	}
	
	public boolean add(String name, Font f)
	{
		return add(name, f, "left");
	}
	
	public boolean add(String name, Font f, String a)
	{
		if(fontlist.containsKey(name))
			return false;
		else
		{
			fontlist.put(name, new Text(f,alignment(a)));
			return true;
		}
		
	}
	
	public boolean remove(String name)
	{
		if(fontlist.containsKey(name))
			return false;
		else
		{
			fontlist.remove(name);
			return true;
		}
	}
	
	public Font getDefaultNormal() {
		return defaultNormal;
	}

	public void setDefaultNormal(Font defaultNormal) {
		this.defaultNormal = defaultNormal;
	}

	public Font getDefaultBold() {
		return defaultBold;
	}

	public void setDefaultBold(Font defaultBold) {
		this.defaultBold = defaultBold;
	}

	public Format(HashMap<String, Text> hm)
	{
		this.fontlist = hm;
	}
	
	public Font getFontNormal(String tagname)
	{
		if(fontlist.containsKey(tagname))
		{
			//System.out.println("NORMAL found: "+tagname+" return: "+fontlist.get(tagname).getFont().getStyle());
			return fontlist.get(tagname).getFont();
		}
		else
		{
			//System.out.println("NORMAL did not find: "+tagname+" return: "+defaultNormal.getStyle());
			return defaultNormal;
		}
	}
	
	public Font getFontBold(String tagname)
	{
		if(fontlist.containsKey(tagname))
		{	
			//System.out.println("BOLD found: "+tagname+" return: "+fontlist.get(tagname).getFont().getStyle());
			return fontlist.get(tagname).getFont();
		}
		else
		{
			//System.out.println("BOLD did not find: "+tagname+" return: "+defaultBold.getStyle());
			return defaultBold;
		}
	}
	
	public int getAlignment(String tagname)
	{
		if(fontlist.containsKey(tagname))
		{
			return fontlist.get(tagname).getAlignment();
		}
		else
			return Paragraph.ALIGN_LEFT;
	}
	
	public Rectangle getPageSize()
	{
		return pagesize;
	}
	
	public static int style(String style)
	{
		if(style.equalsIgnoreCase("bold"))
		{
			return Font.BOLD;
		}
		else if(style.equalsIgnoreCase("italic"))
		{
			return Font.ITALIC;		
		}
		else if(style.equalsIgnoreCase("underline"))
		{
			return Font.UNDERLINE;
		}
		else if(style.equalsIgnoreCase("strikethru"))
		{
			return Font.STRIKETHRU;
		}
		else
			return Font.NORMAL;
	}
	
	/*
	 *	returns Font object equivalent of string 
	 */
	public static Font.FontFamily fontfamily(String fontfamily)
	{
		if(fontfamily.equalsIgnoreCase("zapfdingbats"))
		{
			return Font.FontFamily.ZAPFDINGBATS;
		}
		else if (fontfamily.equalsIgnoreCase("helvetica"))
		{
			return Font.FontFamily.HELVETICA;
		}
		else if (fontfamily.equalsIgnoreCase("courier"))
		{
			return Font.FontFamily.COURIER;
		}
		else if(fontfamily.equalsIgnoreCase("symbol"))
		{
			return Font.FontFamily.SYMBOL;
		}
		else
			return Font.FontFamily.TIMES_ROMAN;			
	}
	
	public static Rectangle pagesize(String ps)
	{
		if(ps.equalsIgnoreCase("a4"))
		{
			return PageSize.A4;
		}
		else if(ps.equalsIgnoreCase("legal"))
		{
			return PageSize.LEGAL;
		}
		else
			return PageSize.LETTER;
	}
	
	public static BaseColor color(String color)
	{
		if(color.equalsIgnoreCase("white"))
		{
			return BaseColor.WHITE;
		}
		else if(color.equalsIgnoreCase("gray"))
		{
			return BaseColor.GRAY;
		}
		else if(color.equalsIgnoreCase("green"))
		{
			return BaseColor.GREEN;
		}
		else if(color.equals("blue"))
		{
			return BaseColor.BLUE;
		}
		else if(color.equals("red"))
		{
			return BaseColor.RED;
		}
		else
			return BaseColor.BLACK;
	
	}
	
	public int alignment(String alignment)
	{
		if(alignment.equalsIgnoreCase("right"))
		{
			return Paragraph.ALIGN_RIGHT;
		}
		else if(alignment.equalsIgnoreCase("justified"))
		{
			return Paragraph.ALIGN_JUSTIFIED;
		}
		else if(alignment.equalsIgnoreCase("center"))
		{
			return Paragraph.ALIGN_CENTER;
		}
		else if(alignment.equalsIgnoreCase("middle"))
		{
			return Paragraph.ALIGN_MIDDLE;
		}
		else
			return Paragraph.ALIGN_LEFT;
	}
	
	public static Font clone(Font f)
	{
		return new Font(f.getFamily(),f.getSize(),f.getStyle(),f.getColor());
	}
	
	
}

class Text
{
	private Font font;
	private int alignment;
	
	public Text(Font f, int a)
	{
		font = f;
		alignment = a;
	}
	
	public Font getFont()
	{
		return font;
	}

	public int getAlignment()
	{
		return alignment;
	}
	
	
}
